<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\PointsGoods;
/**
 * 积分商品控制器
 */
class PointsGoodsController extends BaseController
{
    /**
     * 商品列表(GET) api/pgoods/lists
     * 
     * @return json
     */
    public function lists(Request $request)
    {
        // 商户SESSION数据
        // session(['shopInfo'=>['id'=>1]]); 
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

        // 初始化数组
        $where = $return = array();

        // 构造查询条件
        $where['shop_id']   = $shop_id;         // 本店铺
        $where['delete']    = 0;                // 未删除

        // 查询列表
        $return = PointsGoods::where($where)->orderBy('displayorder')->paginate(10);

        return self::json($return , '查询成功');
        
    }

    /**
     * 商品详情(GET) api/pgoods/details
     *
     * @return json
     */
    public function details(Request $request)
    {
        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id
        // $shop_id = 1;

        if(!$shop_id){
            return self::error('未登录');
        }

        $id = $request->input('id');
        if(!$id){
            return self::error('参数有误');
        }

        $where['shop_id']   = $shop_id;         // 本店铺
        $where['id']        = $id;              // 商品id
        $where['delete']    = 0;                // 未删除

        $return = PointsGoods::where($where)->first();

        if($return){
            return self::json($return , '查询成功');
        }else{
            return self::error('数据为空');
        }

    }
    
    /**
     * 商品添加(POST) api/pgoods/adds
     * @param int $name         商品名称
     * @param int $logo         LOGO
     * @param int $pics         图片
     * @param int $price        销售价
     * @param int $introduce    商品描述
     * @return json
     */
    public function adds(Request $request)
    {
        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

        // 数据整理
        $name           = $request->input('name', '');              // 商品
        $logo           = $request->input('logo', '');              // logo
        $pics           = $request->input('pics', '');              // 商品图片
        $market_price   = $request->input('market_price', '');      // 市场价
        $price          = $request->input('price', '');             // 销售价
        $introduce      = $request->input('introduce', '');         // 商品描述
        $shop_id        = $shop_id;                                 // 店铺id

        // 获取商品排序最大值
        $max = PointsGoods::where('shop_id',$shop_id)->max('displayorder');

        // 实例化model
        $goods = new PointsGoods;

        $goods->name        = $name;
        $goods->logo        = $logo;
        $goods->pics        = $pics;
        $goods->market_price= $market_price;
        $goods->price       = $price;
        $goods->introduce   = $introduce;
        $goods->shop_id     = $shop_id;
        $goods->displayorder= $max+1;

        // 数据入表
        $re = $goods->save();

        if($re){

            return self::success('添加成功');

        }else{

            return self::error('添加失败');

        }   

    }
    
    /**
     * 商品编辑(POST) api/pgoods/edit
     * @param int $id           商品id
     * @param int $name         商品名称
     * @param int $logo         LOGO
     * @param int $pics         图片
     * @param int $price        销售价
     * @param int $introduce    商品描述
     * @return json
     */
    public function edit(Request $request)
    {
        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

        $id = $request->input('id');

        if(!$id){

            return self::error('缺少参数');

        }

        // 初始化数组
        $data = array();

        // 数据整理
        $request->input('name', '')         == null || $data['name']        = $request->input('name');              // 商品
        $request->input('logo', '')         == null || $data['logo']        = $request->input('logo');              // logo
        $request->input('pics', '')         == null || $data['pics']        = $request->input('pics');              // 商品图片
        $request->input('market_price')     == null || $data['market_price']= $request->input('market_price');      // 市场价
        $request->input('price')            == null || $data['price']       = $request->input('price');             // 销售价
        $request->input('introduce')        == null || $data['introduce']   = $request->input('introduce');         // 商品描述
        $data['shop_id'] = $shop_id;

        $re = PointsGoods::where('id',$id)->update($data);

        if($re){

            return self::success('更新成功');

        }else{

            return self::error('更新失败');

        }   

    }

    /**
     * 商品上/下架(POST) api/pgoods/onoff
     * @param int $id           商品id
     * @param int $status       上下架
     */
    public function onOff(Request $request)
    {
        $id     = $request->input('id');
        $status = $request->input('status','1');

        if(!$id){

            return self::error('缺少参数');

        }

        $re = PointsGoods::where('id',$id)->update(['status'=>$status]);

        if($re){

            return self::success('操作成功');

        }else{

            return self::error('操作失败');

        }   

    }

    /**
     * 删除商品(POST) api/pgoods/del
     * @param int $id           商品id
     * @param int $delete       删除
     */
    public function del(Request $request)
    {
        $id     = $request->input('id');
        $delete = $request->input('delete','0');

        if(!$id){

            return self::error('缺少参数');

        }

        $re = PointsGoods::where('id',$id)->update(['delete'=>$delete]);

        if($re){

            return self::success('操作成功');

        }else{

            return self::error('操作失败');

        }   

    }

    /**
     * 商品排序(POST) api/pgoods/chsort
     * @param int $id           商品id
     * @param int $oldPostion   更改前位置   
     * @param int $newPostion   更改后位置
     */
    public function chSort(Request $request)
    {
        $id         = $request->input('id');
        $oldPostion = $request->input('oldPostion');
        $newPostion = $request->input('newPostion');

        if($id<1 || $oldPostion<1 || $newPostion<1){

            return self::error('缺少参数');

        }

        if ($oldPostion == $newPostion) {

            return self::error('无效的位置');

        } 

        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

        // 商品信息
        $info = PointsGoods::where('id',$id)->where('shop_id',$shop_id)->first();

        if (empty($info) || $info['displayorder'] != $oldPostion) {

            return self::error('位置信息有误');

        }

        // 开始排序
        // 下调
        if ($oldPostion > $newPostion) {

            $endNum = $oldPostion - 1;

            $res = PointsGoods::whereBetween('displayorder', [$newPostion, $endNum])
                    ->where('shop_id',$shop_id)
                    ->increment('displayorder');
        }
        
        // 上调
        if ($oldPostion < $newPostion) {

            $beginNum = $oldPostion + 1;

            $res = PointsGoods::whereBetween('displayorder', [$beginNum, $newPostion])
                    ->where('shop_id',$shop_id)
                    ->decrement('displayorder');
        }

        $re = PointsGoods::where('id',$id)->update(['displayorder' => $newPostion]);

        if($re){

            return self::success('操作成功');

        }else{

            return self::error('操作失败');
            
        }

    }


}